package com.iris.live.services.report.handlers;

import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.ReportContext;
import com.iris.live.services.report.data.baseline.BaseUnit;
import com.iris.live.services.report.data.baseline.BaselineContext;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: Tony.Zhang
 * @Date: 2017-10-24 18:25
 **/
public class FinancialFunnelRowHandler extends BaseReportRowHandler{
    @Override
    public void handle(Map reportRow, ReportConfig config, ReportContext context, List<Map> originalDatas, Map<String, Map>  all) {
        if(context instanceof BaselineContext){
            BaselineContext bContext = (BaselineContext) context;
            for(BaseUnit bu : bContext.getUnits()){
                if(bu.getType().equals(BaseUnit.BaseType.TOTAL)){
                    break;
                }
            }

            String code = config.getAinCode();
            if("金融漏斗_进店线索数".equals(code) || "金融漏斗_订单数".equals(code) || "金融漏斗_开票数".equals(code)){
                Double value = 0d;
                if(! (originalDatas.isEmpty())){
                    Object oValue = originalDatas.get(0).get(config.getMatchKey());
                    if(StringUtils.isNotNullOrBlank(oValue)){
                        value = Double.valueOf(oValue.toString());
                    }
                }

                Collection<Map> cells = (Collection<Map>)reportRow.values();
                for(Map cell : cells){
                    cell.put(config.getCategory() + "Value", value);
                }
            }
        }
    }
}
